package com.hlz.rabbit.mq.ack;

import com.hlz.rabbit.mq.util.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * @author Hongliang Zhu
 * @create 2022-11-25 22:04
 */
public class AckC1 {
    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbitMqUtils.getChannel();

        DeliverCallback deliverCallback = (consumerTag, res) -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("消费者1收到消息： " + new String(res.getBody()));
            channel.basicAck(res.getEnvelope().getDeliveryTag(), false);
        };
        channel.basicConsume("ack_queue",false, deliverCallback, new CancelCallback() {
            @Override
            public void handle(String consumerTag) throws IOException {
                System.out.println("C1->消息取消...");
            }
        });

    }
}
